<template>
  <div class="container">
    <el-card shadow="never">
      <!-- 搜索 -->
      <el-form :model="articlesParams">
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item label="关键字：" prop="keyword">
              <el-input
                v-model="articlesParams.keyword"
                style="width:180px;"
                placeholder="根据文章标题搜索"
                @keyup.enter="handleFilter"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="状态：" prop="state">
              <el-select
                v-model="articlesParams.state"
                class="filter-item"
                style="width: 180px;"
                placeholder="请选择"
                @keyup.enter="handleFilter"
              >
                <el-option
                  v-for="item in status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button
                size="small"
                @click="resetForm"
              >清除</el-button>
              <el-button
                size="small"
                type="primary"
                @click="handleFilter"
              >搜索</el-button>
              <!-- <el-button class="filter-item" size="small" type="primary" @click="resetForm">重置</el-button> -->
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align:right">
            <el-button type="success" @click="add">
              <i class="el-icon-edit" />
              新增技巧
            </el-button>
          </el-col>
        </el-row>

      </el-form>
      <!-- 数据记录 -->
      <el-alert
        :title="'共' + total + '条记录'"
        type="info"
        left
        show-icon
        style="margin-bottom:15px;"
      />
      <!-- 数据列表 -->
      <el-table :data="articlesList">
        <el-table-column label="序号" type="index" />
        <el-table-column label="文章标题">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <svg-icon v-if="scope.row.videoURL" icon-class="documentation" @click.native="playViedo(scope.row.videoURL)" />
          </template>
        </el-table-column>
        <el-table-column label="阅读数">
          <template slot-scope="scope">
            {{ scope.row.visits }}
          </template>
        </el-table-column>
        <el-table-column label="录入人">
          <template slot-scope="scope">
            {{ scope.row.username }}
          </template>
        </el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state==1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="hPreview(scope.row)">预览</el-button>
            <el-button v-if="scope.row.state==1" type="text" @click="hDisable(scope.row)">禁用</el-button>
            <el-button v-else type="text" @click="hDisable(scope.row)">启用</el-button>
            <el-button type="text" :disabled="scope.row.state==1" @click="modify(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state==1" @click="hDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination" style="text-align:right">
        <div class="pages">
          <el-pagination
            background
            :current-page="Number(articlesParams.page)"
            :total="Number(total)"
            :page-size="Number(articlesParams.pagesize)"
            :page-sizes="[5,10,20, 50]"
            layout="prev, pager, next,sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <!-- 预览弹框 子组件 -->
    <el-dialog
      width="60%"
      title="预览文章"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <articles-preview v-if="article&&articleFlag" :content="article" />
    </el-dialog>
    <!-- 新增弹框 -->
    <el-dialog
      v-if="showVisible"
      width="40%"
      :title="isEdit?'修改文章':'新增文章'"
      :visible.sync="showVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      class="Dialogvideo"
    >
      <articles-add :form-data="requestData" :articles-params="articlesParams" :total="total" @close="hClose" @submit="loadSubmit" />
    </el-dialog>
    <!-- 视频弹框 -->
    <el-dialog
      v-if="videoState"
      width="40%"
      title="视频"
      :visible.sync="videoState"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :modal="false"
      @close="hClose"
    >
      <div v-if="videoState == true" class="videomasks">

        <video :src="src" controls="controls" class="video" autoplay />
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants'
import { list, detail, remove, changeState } from '@/api/hmmm/articles'
import articlesPreview from '../components/articles-preview.vue'
import articlesAdd from '../components/articles-add.vue'

export default {
  components: { articlesPreview, articlesAdd },
  data() {
    return {
      src: '',
      videoState: false,
      isEdit: false,
      showVisible: false,
      article: null,
      articleFlag: false,
      showDialog: false,
      total: null,
      articlesParams: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      articlesList: [],
      requestData: {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      }
    }
  },
  computed: {
    status() {
      return status
    }
  },
  created() {
    this.getList()
  },
  methods: {
    playViedo(src) {
      this.src = src
      this.videoState = true
    },
    loadSubmit() {
      this.articlesParams.page = Math.ceil(++this.total / this.articlesParams.pagesize)
      this.getList()
    },
    add() {
      this.showVisible = true
      this.isEdit = false
    },
    // 当点击了预览
    hPreview(art) {
      this.article = art
      // console.log(this.article)
      this.articleFlag = true
      this.showDialog = true
    },
    handleFilter() {
      this.articlesParams.page = 1
      this.getList()
    },
    resetForm() {
      this.articlesParams = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
      this.getList()
    },
    async hDisable(row) {
      row.state = row.state ? 0 : 1
      const res = await changeState({
        id: row.id,
        state: row.state
      })
      console.log(res)
    },
    // 获取数据
    async getList() {
      const res = await list(this.articlesParams)
      console.log(res, 'res')
      this.articlesList = res.data.items
      this.total = res.data.counts
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.articlesParams.pagesize = val
      if (this.articlesParams.page === 1) {
        this.getList(this.articlesParams)
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.articlesParams.page = val
      this.getList(this.articlesParams)
    },
    async modify(row) {
      const res = await detail(row)
      console.log(res)
      this.requestData.title = res.data.title
      this.requestData.id = res.data.id
      this.requestData.articleBody = res.data.articleBody
      this.requestData.videoURL = res.data.videoURL
      this.isEdit = true
      this.showVisible = true
    },
    async hDel(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
      // 判断是否删除的最后一个数据
        if (this.articlesList.length === 1) {
          this.articlesParams.page--
          if (this.articlesParams.page === 0) {
            this.articlesParams.page++
          }
        }
        await remove(row)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hClose() {
      this.showVisible = false
      this.requestData = {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      }
    }

  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog {
  width: 800px !important;
}
video {
  width: 100%;
}
.svg-icon {
  color: skyblue;
  cursor: pointer;
}
</style>
